package cn.iocoder.an.module.mold.controller.admin.upkeepset;


import cn.hutool.core.collection.CollUtil;
import cn.iocoder.an.framework.common.pojo.CommonResult;
import cn.iocoder.an.framework.common.pojo.PageResult;
import cn.iocoder.an.framework.common.util.object.BeanUtils;
import cn.iocoder.an.module.mold.controller.admin.account.vo.AccountPageReqVO;
import cn.iocoder.an.module.mold.controller.admin.account.vo.AccountRespVO;
import cn.iocoder.an.module.mold.controller.admin.upkeepcat2item.vo.UpkeepCat2itemRespVO;
import cn.iocoder.an.module.mold.controller.admin.upkeepcategory.vo.UpkeepCategoryRespVO;
import cn.iocoder.an.module.mold.controller.admin.upkeepitem.vo.UpkeepItemRespVO;
import cn.iocoder.an.module.mold.controller.admin.upkeepset.vo.UpkeepSetRespVO;
import cn.iocoder.an.module.mold.dal.dataobject.account.AccountDO;
import cn.iocoder.an.module.mold.dal.dataobject.upkeepcat2item.UpkeepCat2itemDO;
import cn.iocoder.an.module.mold.dal.dataobject.upkeepcategory.UpkeepCategoryDO;
import cn.iocoder.an.module.mold.dal.dataobject.upkeepitem.UpkeepItemDO;
import cn.iocoder.an.module.mold.service.account.AccountService;
import cn.iocoder.an.module.mold.service.upkeepcat2item.UpkeepCat2itemService;
import cn.iocoder.an.module.mold.service.upkeepcategory.UpkeepCategoryService;
import cn.iocoder.an.module.mold.service.upkeepitem.UpkeepItemService;
import com.fhs.core.trans.anno.TransMethodResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import static cn.iocoder.an.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 模具保养设置")
@RestController
@RequestMapping("/mold/upkeep-set")
@Validated
public class UpkeepSetControler {

    @Resource
    private AccountService accountService;

    @Resource
    private UpkeepCategoryService upkeepCategoryService;

    @Resource
    private UpkeepCat2itemService upkeepCat2itemService;

    @Resource
    private UpkeepItemService upkeepItemService;

    @GetMapping("/page")
    @Operation(summary = "获得模具台账分页")
    @PreAuthorize("@ss.hasPermission('mold:upkeep-set:query')")
    @TransMethodResult
    public CommonResult<PageResult<AccountRespVO>> getAccountPage(@Valid AccountPageReqVO pageReqVO) {
        PageResult<AccountDO> pageResult = accountService.getAccountPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AccountRespVO.class));
    }

    @GetMapping("/setCat")
    @Operation(summary = "获取模具保养类型配置详情")
    @Parameter(name = "catId", description = "保养类型Id", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('mold:upkeep-set:set')")
    @TransMethodResult
    public CommonResult<UpkeepSetRespVO> getCategoryDetails(@RequestParam("catId") Long catId) {
        // 创建返回对象
        UpkeepSetRespVO result = new UpkeepSetRespVO();

        // 获取保养类型详情
        UpkeepCategoryDO category = upkeepCategoryService.getUpkeepCategory(catId);
        result.setCategoryDetail(BeanUtils.toBean(category, UpkeepCategoryRespVO.class));

        // 获取保养项目明细
        List<UpkeepCat2itemDO> items = upkeepCat2itemService.getUpkeepCat2itemByCatId(catId);

        // 从items中提取itemId列表
        List<Long> itemIds = items.stream()
                .map(UpkeepCat2itemDO::getItemId)
                .filter(Objects::nonNull)
                .collect(Collectors.toList());

        // 根据itemId列表查询UpkeepItemDO列表
        List<UpkeepItemDO> itemDetails = new ArrayList<>();
        if (CollUtil.isNotEmpty(itemIds)) {
            itemDetails = upkeepItemService.getUpkeepItemByIds(itemIds);
        }

        result.setItemDetails(BeanUtils.toBean(itemDetails, UpkeepItemRespVO.class));
        return success(result);
    }



    @PutMapping("/updateCat")
    @Operation(summary = "更新保养类型配置")
    @PreAuthorize("@ss.hasPermission('mold:upkeep-set:update')")
    public CommonResult<Boolean> updateCatId(@RequestParam("moldId") Long moldId, @RequestParam("catId") Long catId) {
        //通过接收模具ID以及保养类型ID进行更新
        if  (catId == null) {
            return success(true);
        }

        accountService.updateAccountCatId(moldId, catId);
        return success(true);
    }

}
